package top.whysu.edevice.service.device.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.whysu.edevice.dao.device.RecordSensorDao;
import top.whysu.edevice.dto.device.RecordSensorQueryDTO;
import top.whysu.edevice.dto.page.PagenationDataDTO;
import top.whysu.edevice.po.device.RecordSensorPO;
import top.whysu.edevice.service.device.RecordSensorService;
import top.whysu.edevice.utils.PageUtils;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 传感器上报记录
 */
@Service("SensorServiceImpl")
public class RecordSensorServiceImpl implements RecordSensorService {
    private final Logger LOGGER = LoggerFactory.getLogger(RecordSensorServiceImpl.class);

    @Resource
    private RecordSensorDao recordSensorDao;

    @Override
    public PagenationDataDTO<RecordSensorPO> selectPage(RecordSensorQueryDTO queryDTO) {
        // 获取集合
        LinkedHashMap queryMap = JSON.parseObject(JSON.toJSONString(queryDTO), LinkedHashMap.class);
        // limit的起始值
        queryMap.put("rowIndex", PageUtils.getRowIndex(queryDTO.getPageNum(), queryDTO.getPageSize()));
        List<RecordSensorPO> list = recordSensorDao.queryAllByLimit(queryMap);
        // 获取总数
        long totalSize = recordSensorDao.count(queryMap);
        if (CollectionUtils.isNotEmpty(list) && totalSize > 0) {
            return new PagenationDataDTO<>(totalSize, queryDTO.getPageNum(), queryDTO.getPageSize(), list);
        } else {
            return PageUtils.toPagedResult(null);
        }
    }
}